# -*- coding: utf-8 -*-

import ConfigParser
import datetime
import json

import dateutil.parser
from pymongo import MongoClient

import common


def main():
    config = ConfigParser.RawConfigParser()
    config.read("config/leaders.ini")

    use_proxy = config.getboolean("default", "proxy")
    common.prepare(use_proxy=use_proxy)

    client = MongoClient()
    db = client.leaders

    date = datetime.date(2015, 12, 06)
    today = datetime.date.today()

    leader = "xjp"
    url = "http://cpc.people.com.cn/data/%s/%%s.json" % leader

    while date <= today:
        print date

        while True:
            try:
                raw = common.simple_read(url % date.strftime("%Y%m%d")).strip()
                if raw:
                    d = json.loads(raw)

                    for item in d:
                        item[u"input_date"] = dateutil.parser.parse(item[u"input_date"])
                        db[leader].insert_one(item)

                date += datetime.timedelta(days=1)

                common.random_sleep(1)
                break

            except Exception, e:
                print "An exception occurred."
                print e

            common.random_sleep(5)


if __name__ == '__main__':
    while True:
        try:
            main()
            break
        except Exception, e:
            print e
